import Vue from "vue"
export const mutations = {
	// 更新歌曲
	upDateSong(state, music) {
		state.song = music
		state.play = true;
		Vue.prototype.$audioContext.src = state.song.src
		Vue.prototype.$audioContext.autoplay = true

		uni.setStorageSync("song", music)
		var result = state.list.find(item => {
			return music.id === item.id
		})
		if (result == undefined) {
			var newsong = {
				name: music.name,
				id: music.id,
				author: music.author
			}
			let arr = []
			arr.push(newsong)
			this.commit("addList", arr)
		}
	},
	// 更新播放状态
	upDateStatus(state) {
		state.play = !this.state.play;
		if (state.play) {
			// console.log("执行播放")
			Vue.prototype.$audioContext.play()
		} else {
			// console.log("执行暂停")
			Vue.prototype.$audioContext.pause()
		}
	},
	// 获取到audio 上下文 innerAudioContext 对象 ,在App.vue中传入
	// upDateContext(state, innerAudioContext) {
	// 	state.innerAudioContext = innerAudioContext;
	// },
	// 添加到播放列表
	addList(state, list) {
		state.index = state.list.length
		state.list.push(...list)
		this.dispatch("getSong", state.list[state.index].id)
		uni.setStorageSync("list", state.list)
	},
	// 下一首
	next(state, model) {
		if (model % 3 == 1 || model % 3 == 2) {
			if (state.index == state.list.length - 1) {
				state.index = 0
			} else {
				state.index++
			}
		} else {
			state.index = Math.ceil(Math.random() * state.list.length)
		}
		this.dispatch("getSong", state.list[state.index].id)
	},
	// 上一首
	prev(state, model) {
		if (model % 3 == 1 || model % 3 == 2) {
			if (state.index == 0) {
				state.index = state.list.length - 1
			} else {
				state.index--
			}
		} else {
			state.index = Math.ceil(Math.random() * state.list.length)
		}
		this.dispatch("getSong", state.list[state.index].id)
	},
	// 点击播放列表中的歌曲，更新index
	upDateIndex(state, index) {
		state.index = index
	},
	// 删除歌曲列表中的歌曲
	delete(state, index) {
		state.list.splice(index, 1);
		if (state.list == 0) {
			state.index = 0;
		} else if (index == state.index) {
			if (index == state.list.length - 1) {
				state.index = 0;
			} else {
				state.index = index;
			}
			this.dispatch("getSong", state.list[state.index].id)
		}
		uni.setStorageSync("list", state.list)
	},
	deleteAll(state) {
		state.list = []
	}

}
